using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Elementum
{
    public class Mechine : SensorSwitch
    {
        protected int _count;
        Animation _work;
        Animation _number;
        Animation _error;
       

        bool _needActive = false;

        public delegate void ActionHandler();
        public event ActionHandler AdditionAction;

        int _capacity;

        public int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable[SaveMechineID]._count = value;
            }
        }

        public Mechine(string id, World world, Stage stage)
            : base(world, stage)
        {
            SaveMechineID = id;
            _body.SetUserData("machine");

            if (!StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable.ContainsKey(SaveMechineID))
            {
                MechineState mechineState = new MechineState();
                try
                {
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable.Add(SaveMechineID, mechineState);
                }
                catch (ArgumentException)
                {
                    throw new Exception("An Mechine with Key = " + SaveMechineID + " already exists.");
                }
            }
            _work = new Animation("images/items/mechineWork",new Point(88,86));
            _work.LayerDepth = 0.85f;
            _number = new Animation("images/items/mechineNumber");
           
            _number.LayerDepth = 0.75f;
            _error = new Animation("images/items/mechineError", new Point(90, 88));
            _error.LayerDepth = 0.85f;
            CurrentAnimation = _error;
            Size = new Vector2(92, 80);
        }


        public string SaveMechineID
        {
            get;
            set;
        }

        public override void Restart()
        {
           // base.Restart();
            if (StageManager.Save.MechineList.Contains(SaveMechineID)) {
                StageManager.Save.MechineList.Remove(SaveMechineID);
            }
        }

        public int Capacity
        {
            get { return _capacity; }
            set
            {
                _capacity = value;
                Count = StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable[SaveMechineID]._count;
                StageManager.Save.StageStateTable[StageManager.CurrentStateID].MechineStateTable[SaveMechineID]._capacity = value;
                if (Count == Capacity)
                    _needActive = true;
            }
        }

        public override void OnDraw(SpriteBatch spriteBatch, Matrix transform)
        {
            _transform = transform;

            //
            float ps = 1f - ((float)Count / (float)Capacity);

            Rectangle area = new Rectangle();

            switch (Capacity - Count)
            {
                case 1:
                    area = new Rectangle(0,0,18,25);//1
                    break;
                case 2: area = new Rectangle(18,0,19,25);//2
                    break;

                case 3: area = new Rectangle(37,0,18,26);//3
                    break;

                case 4:
                    area = new Rectangle(0,26,18,24);//4
                    break;
                case 5:
                    area = new Rectangle(18,26,16,30);//5
                    break;
                case 6:
                    area = new Rectangle(36,26,16,30);//6
                    break;
            }

            if(GameClass.Random.Next(64)<10)
                area = new Rectangle(0, 56, 36, 33);//element

            if (Capacity - Count == 0)
                _work.OnDraw(spriteBatch, Position, transform);
            else
            {
                _error.OnDraw(spriteBatch, Position, transform);
                _number.OnDraw(spriteBatch, Position-new Vector2(0,15), transform, area);
            }
            //_back.OnDraw(spriteBatch, Position + new Vector2(0, -40 + _back._frameSize.Y * ps / 2), transform, drawArea);
            //if (jij)

        }

        public override void Update(int delta)
        {
            float ps = 1f - ((float)Count / (float)Capacity);
            int top = (int)(_number._frameSize.Y * ps);
            //drawArea = new Rectangle(0, 0, _back._frameSize.X, (int)(_back._frameSize.Y * ps));

            //_back.Scale = Scale;
            //_back.Rotation = Rotation;
            //_back.Alpha = Alpha;
            //_back.Update(delta);

            _number.Scale = Scale;
            _number.Rotation = Rotation;
            _number.Alpha = Alpha;
            _number.Update(delta);

            _error.Scale = Scale;
            _error.Rotation = Rotation;
            _error.Alpha = Alpha;
            _error.Update(delta);

            if (_needActive)
            {
                ActiveTarget();
                _needActive = false;
            }

        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.BeginContactAction(contact, contactBody, normal);

            if (contactBody.GetUserData() == "ElementPiece")
            {
                ElementPiece e = (ElementPiece)contactBody.Owner;
                //  
                if (Count < Capacity)
                    Count++;
                ActiveTarget();
            }
        }

        private void ActiveTarget()
        {
            if (Count == Capacity)
            {
                if (!_isActive)
                {
                    if (AdditionAction != null)
                        AdditionAction();
                    foreach (ChangeStateable _s in _targetList)
                    {
                        _s.ChangeState();
                        _isActive = true;
                    }
                    if (!StageManager.Save.MechineList.Contains(SaveMechineID))
                        StageManager.Save.MechineList.Add(SaveMechineID);
                }
            }
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.EndContactAction(contact, contactBody, normal);
            //if (contactBody.GetUserData() == "ElementPiece")
            //if (_isActive)
            //    foreach (ChangeStateable _s in _targetList)
            //    {
            //        _isActive = false;
            //        _s.ChangeState();
            //        //player.BeginContact -= ChangeSpotLightStage;
            //    }
        }

    }
}
